package com.sky.controller.admin;

import com.sky.Entity.Employee;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeQueryListVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("收到登录请求：" + employeeLoginDTO);
        Employee login = employeeService.login(employeeLoginDTO);
        log.info("查询到员工信息：" + login);

        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.EMP_ID, login.getId());
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, jwtProperties.getAdminSecretKey())
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + jwtProperties.getAdminTtl()))
                .compact();
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(login.getId())
                .userName(login.getUsername())
                .name(login.getName())
                .token(jwt)
                .build();

        return Result.success(employeeLoginVO);
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping()
    @ApiOperation("新增员工")
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.add(employeeDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询员工信息")
    public Result<EmployeeQueryListVo> query(@RequestParam(required = false) String name
            , @RequestParam(required = true) Integer page
            , @RequestParam(required = true) Integer pageSize) {
        EmployeeQueryListVo query = employeeService.query(name,page,pageSize);
        return Result.success(query);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改员工状态")
    public Result<String> updateStatus(@RequestParam(required = true) Long id,@PathVariable(required = true) Integer status){
        employeeService.updateStatus(id,status);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> queryById(@PathVariable Long id){
        Employee employee=employeeService.queryById(id);
        return Result.success(employee);
    }
    @PutMapping
    @ApiOperation("修改用户信息")
    public Result<String> modify(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
